// 引入express
const express = require('express')
const app = express()
// 引入cors
const cors = require('cors')
// 引入路由器
const hostelDetail = require('./routes/hostelDetail')
const hostelServices = require('./routes/hostelServices')
const hostelOrder = require('./routes/hostelOrder')
const latestNewsRouter = require('./routes/latestNews')
const latestnewsTypeRouter = require('./routes/latestnewsType')
const newsDetailsRouter = require('./routes/newsDetails')
const hostelListRouter = require('./routes/hostelList')
const hostelNoticeRouter = require('./routes/hostelNotice')
const hostelTypeRouter = require('./routes/hostelType')
const hostelNavRouter = require('./routes/hostelNav')
const CooperationRouter = require('./routes/Cooperation')
const userRouter = require('./routes/User')
const Orderinfor = require('./routes/orderInFormation')
const hostelPublickgoodRouter = require('./routes/hostelPublicGood')
const memberCardRouter = require('./routes/memberCard')
const AboutmeRouter = require('./routes/Aboutme')

// 端口号
const port = 5000

// 托管静态资源
app.use(express.static(__dirname + '/public'))

// 将post传参转为对象
app.use(
  express.urlencoded({
    extended: true,
  })
)

// 请求体参数是json结构  {name: tom, pwd: 123}
app.use(express.json())

// cors跨域
app.use(cors())

// 路由器使用
app.use('/hostelList', hostelListRouter)
app.use('/hostelNotice', hostelNoticeRouter)
app.use('/hostelType', hostelTypeRouter)
app.use('/hostelNav', hostelNavRouter)
app.use('/hostelDetail', hostelDetail)
app.use('/hostelServices', hostelServices)
app.use('/hostelOrder', hostelOrder)
app.use('/Cooperation', CooperationRouter)
app.use('/user', userRouter)
app.use('/order', Orderinfor)
app.use('/hostelPublicGood', hostelPublickgoodRouter)
app.use('/memberCard', memberCardRouter)
app.use('/latestNews', latestNewsRouter)
app.use('/latestnewsType', latestnewsTypeRouter)
app.use('/newsDetails', newsDetailsRouter)
app.use('/aboutme', AboutmeRouter)

// 监听端口
app.listen(port, () => console.log(`localhost:${port}`))

// 错误处理中间件
app.use((err, req, res, next) => {
  console.log(err)
  res.status(500).send({ code: 500, msg: '服务器端错误' })
})
